package com.ezjie.ielts.module_word.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ezjie.ielts.R;
import com.ezjie.ielts.model.Instance;
import com.ezjie.ielts.util.PlayAnimationUtils;
import com.ezjie.ielts.util.TextUtil;

public class ExampleSentenceAdapter extends BaseAdapter {

	private Context mContext;
	private List<Instance> list;
	private String keyWord;
	private AnimationDrawable animation;

	public ExampleSentenceAdapter(Context mContext) {
		this.mContext = mContext;
	}

	public void setList(List<Instance> list, String keyWord) {
		this.list = list;
		this.keyWord = keyWord;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list == null ? 0 : list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder hv;
		animation = PlayAnimationUtils.PlayWordAnimation(mContext);
		if (view == null) {
			view = LayoutInflater.from(mContext).inflate(R.layout.example_sentence_item, null);
			hv = new ViewHolder();
			hv.translate = (TextView) view.findViewById(R.id.translate);
			hv.sentence = (TextView) view.findViewById(R.id.sentence);
			hv.playImg = (ImageView) view.findViewById(R.id.play_img);
			hv.itemLayout = (RelativeLayout) view.findViewById(R.id.list_item_rl);
			view.setTag(hv);
		} else {
			hv = (ViewHolder) view.getTag();
		}

		if (list != null && list.size() != 0) {
			String translate = list.get(position).getTranslation();
			String sentence = list.get(position).getOrigin();
			hv.translate.setText(translate);
			hv.sentence.setText(TextUtil.transferBoldWhiteWord(keyWord, sentence));
		}
		return view;
	}

	class ViewHolder {
		RelativeLayout itemLayout;
		TextView sentence;
		TextView translate;
		ImageView playImg;
	}

}
